<template>
  <div>
    <el-card>
      <el-button type="success" size="large" round @click="dialogVisible = true">添加用户</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="用户名称">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <span style="margin-left: 10px">{{ scope.row.role.roleName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户名">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <span style="margin-left: 10px">{{ scope.row.username }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" round @click="handleEdit(scope.row)" >编辑</el-button>
            <el-popconfirm title="你确定要删除吗" confirm-button-text="确认" cancel-button-text="取消"
              @confirm="handleDelete(scope.row)">
              <template #reference>
                <el-button type="danger" round >删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="添加用户" @close="addConfirm" style="border-radius: 20px; font-weight: bold;"
      :center="true">
      <el-form :model="addFrom">
        <el-form-item label="用户名: " prop="username" label-width="80px">
          <el-input v-model="addFrom.username" autocomplete="on" />
        </el-form-item>
        <el-form-item label="密码：" prop="password" label-width="80px">
          <el-input v-model="addFrom.password" autocomplete="on" />
        </el-form-item>
        <el-form-item label="角色：" prop="role" label-width="80px">
          <el-select v-model="addFrom.role" clearable placeholder="请选择角色" style="width: 240px">
            <el-option v-for="item in roleList" :key="item._id" :label="item.roleName" :value="item._id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addConfirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="dialogUpdateVisible" title="更新角色" @close="updateConfirm" style="border-radius: 20px; font-weight: bold;" :center="true">
      <el-form :model="updateForm" @keyup.enter="updateConfirm">
        <el-form-item label="用户名：" prop="username" label-width="80px">
          <el-input v-model="updateForm.username" autocomplete="on" label-width="80px"/>
        </el-form-item>
        <el-form-item label="密码：" prop="password" label-width="80px">
          <el-input v-model="updateForm.password" autocomplete="on" />
        </el-form-item>
        <el-form-item label="角色：" prop="role" label-width="80px">
          <el-select v-model="updateForm.role" clearable placeholder="请选择角色" style="width: 240px">
            <el-option v-for="item in roleList" :key="item._id" :label="item.roleName" :value="item._id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogUpdateVisible = false">取消</el-button>
          <el-button type="primary" @click="updateConfirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import axios from 'axios'
import { onMounted, ref, reactive } from 'vue';
const tableData = ref([])
const roleList = ref([])
//记录更新的详细，用于传给后端
const updateList = ref({})
const dialogVisible = ref(false)
const dialogUpdateVisible = ref(false)
//记录更新的信息，用于渲染
const updateForm = reactive({
  username: '',
  password: '',
  role: ''
})
const addFrom = reactive({
  username: '',
  password: '',
  role: ''
})
const handleEdit = (item/*当前表格行的数据*/) => {
  // console.log('edit',item);
  dialogUpdateVisible.value = true
  updateForm.username = item.username
  updateForm.password = item.password
  updateForm.role = item.role._id
  updateList.value = item
}
const handleDelete = async (item) => {
  await axios.delete(`/admin/users/${item._id}`)
  await getUserList()
}
//发请求给后端改变数据
const addConfirm = async () => {
  dialogVisible.value = false
  await axios.post("/admin/users",addFrom)
  await getUserList()
  addFrom.password = ""
  addFrom.username = ""
  addFrom.role = ""
}
const updateConfirm = async () => {
  dialogUpdateVisible.value = false
  // console.log(updateList.value._id,updateForm);
  await axios.put(`/admin/users/${updateList.value._id}`,updateForm)
  await getUserList()
}
onMounted(() => {
  getUserList()
  getRoleList()
})
const getUserList = async () => {
  const res = await axios.get('/admin/users')
  tableData.value = res.data
}
const getRoleList = async () => {
  const res = await axios.get('/admin/roles')
  roleList.value = res.data
}
</script>
<style scoped lang='scss'>
.el-card {
  // background-color: aqua;
  border-radius: 30px;

  :deep(.el-table .cell) {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: bold;
  }
}
</style>